<template>
  <!-- 登录窗口 -->
  <div class="login-content-box">
    <div class="login-content">
      <div class="login-content-close">
        <span
          class="iconfont icon-guanbi"
          @click="loginModalStore.visible = false"
        ></span>
      </div>

      <div class="login-plane">
        <div class="login-plane-left">
          <p class="qrcode-tip">扫描二维码登录</p>
          <div class="login-qrcode"></div>
          <div class="login-qrcode-text">
            <p>请使用<span>哔哩哔哩客户端</span></p>
            <p>扫码登录或扫码下载APP</p>
          </div>
        </div>
        <div class="login-line"></div>
        <div class="login-plane-right">
          <div class="login-plane-title">
            <div
              :class="{ 'login-type-active': passwordLoginType }"
              @click="
                passwordLoginType = true;
                phoneLoginType = false;
              "
            >
              密码登录
            </div>
            <div class="line"></div>
            <div
              :class="{ 'login-type-active': phoneLoginType }"
              @click="
                passwordLoginType = false;
                phoneLoginType = true;
              "
            >
              短信登录
            </div>
          </div>
          <div class="login-text" v-if="passwordLoginType">
            <div class="input-box">
              <span>账号</span>
              <input
                v-model="loginDataFrom.username"
                class="username-plane"
                placeholder="请输入账号"
              />
            </div>
            <div class="line"></div>
            <div class="input-box">
              <span>密码</span>
              <input
                v-model="loginDataFrom.password"
                class="password-plane"
                placeholder="请输入密码"
              />
              <span class="forget-lable">忘记密码？</span>
            </div>
          </div>
          <div class="login-button" v-if="passwordLoginType">
            <div class="register-but">注册</div>
            <!-- <div class="login-but"></div> -->
            <el-button
              class="h40px w194px bg-#00aeec c-#fff b-rd-8px"
              @click="login"
              :loading="loginButLoading"
            >
              登录</el-button
            >
          </div>

          <div class="login-text" v-if="phoneLoginType">
            <div class="input-box">
              <span class="type-label">+86</span>
              <input class="username-plane" placeholder="请输入手机号" />
              <span class="forget-lable get-code-lable">获取验证码</span>
            </div>
            <div class="line"></div>
            <div class="input-box">
              <span class="type-label">验证码</span>
              <input class="password-plane" placeholder="请输入验证码" />
            </div>
          </div>
          <div class="login-button phone" v-if="phoneLoginType">
            <div class="login-but">登录/注册</div>
          </div>

          <p class="orther-login-tip">其他方式登录</p>
          <div class="orther-login-link">
            <div>
              <span class="iconfont icon-weixin"></span>
              <span>微信登录</span>
            </div>
            <div>
              <span class="iconfont icon-weibo"></span>
              <span>微博登录</span>
            </div>
            <div>
              <span class="iconfont icon-qq"></span>
              <span>QQ登录</span>
            </div>
          </div>
        </div>
      </div>

      <div class="login-tip">
        <p>未注册过哔哩哔哩的手机号，我们将自动帮你注册账号</p>
        <p>
          登录或完成注册即代表你同意
          <span>用户协议</span>
          或
          <span>隐私政策</span>
        </p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { LoginData } from "@/api/user/types";
import { useUserStore } from "@/store/userStore";
import { useLoginModalStore } from "@/store/loginModalStore";
import to from "await-to-js";
import { ref } from "vue";

const userStore = useUserStore();
const loginModalStore = useLoginModalStore();

const passwordLoginType = ref(true); // 密码登录类型
const phoneLoginType = ref(false); // 短信登录类型

const loginButLoading = ref(false); // 登录按钮加载状态

// 登录表单
const loginDataFrom = ref<LoginData>({
  username: "",
  password: "",
});

// 用户登录
const login = async () => {
  if (
    loginDataFrom.value.username === "" ||
    loginDataFrom.value.password === ""
  ) {
    ElMessage.error("请输入账号或密码");
    return;
  }

  loginButLoading.value = true; // 开始加载
  const [err] = await to(userStore.login(loginDataFrom.value));
  if (!err) {
    await userStore.getInfo();
    loginModalStore.visible = false;
    loginDataFrom.value.username = "";
    loginDataFrom.value.password = "";
  }
  loginButLoading.value = false; // 停止加载
};
</script>

<style scoped lang="less">
.center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.login-content-box {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 5;
  background-color: rgba(0, 0, 0, 0.5);

  .login-content {
    .center;
    width: 820px;
    min-height: 430px;
    background: #fff;
    border-radius: 8px;
    padding: 65px 65px 29px 92px;
    box-sizing: border-box;
    background-image: url(../img/home/login-bag-left.png),
      url(../img/home/login-bag-right.png);
    background-position: 0 100%, 100% 100%;
    background-repeat: no-repeat, no-repeat;
    background-size: 14%;
    color: #505050;

    .login-content-close {
      position: absolute;
      right: 20px;
      top: 20px;
      span {
        cursor: pointer;
        font-size: 30px;
      }
    }

    .login-plane {
      display: flex;
      justify-content: space-between;

      .login-plane-left {
        display: flex;
        row-gap: 30px;
        flex-direction: column;
        align-items: center;
        font-size: 13px;
        justify-content: space-between;
        .qrcode-tip {
          font-size: 18px;
        }
        .login-qrcode {
          background-image: url(../assets/img/home/login-qrcode-bag.png);
          height: 173px;
          width: 330px;
          background-size: 100% 100%;
          position: absolute;
          left: 0px;
          top: 114px;
        }

        .login-qrcode-text {
          p {
            margin-bottom: 5px;
          }
          span {
            color: #00aeec;
            margin: 0 5px;
          }
        }
      }

      .login-line {
        border: 0.5px solid #e3e5e7;
      }

      .login-plane-right {
        width: 400px;
        display: flex;
        flex-direction: column;
        align-items: center;
        row-gap: 20px;

        .login-plane-title {
          font-size: 18px;
          display: flex;
          gap: 40px;
          cursor: pointer;
          div {
            line-height: 20px;
          }
          .line {
            border-left: 1px solid #e3e5e7;
            cursor: default;
          }
          .login-type-active {
            color: #00aeec;
          }
        }

        input:focus {
          outline: none;
          border: none;
        }

        .login-text {
          width: inherit;
          border: 1px solid #e3e5e7;
          border-top-left-radius: 10px;
          border-top-right-radius: 10px;
          border-bottom-left-radius: 10px;
          border-bottom-right-radius: 10px;

          .line {
            border-top: 1px solid #e3e5e7;
          }

          .input-box {
            box-sizing: border-box;
            padding: 12px 20px;
            display: flex;
            column-gap: 12px;

            .type-label {
              width: 50px;
            }

            .get-code-lable {
              border-left: 1px solid #e3e5e7;
              padding-left: 20px;
              cursor: pointer;
            }

            input {
              flex: 1;
              font-size: 14px;
              line-height: 20px;
              border: 0;
            }

            .forget-lable {
              color: #00aeec;
            }
          }
        }

        .login-button {
          display: flex;
          width: 400px;
          justify-content: space-between;
          div {
            height: 40px;
            width: 194px;
            border-radius: 8px;
            text-align: center;
            line-height: 40px;
            border: 1px solid #e3e5e7;
            box-sizing: border-box;
            cursor: pointer;
          }

          .register-but {
            background-color: #ffffff;
          }

          .login-but {
            color: #ffffff;
            background-color: #00aeec;
          }

          .login-but:hover {
            background-color: rgba(0, 174, 236, 0.9);
          }
        }

        .phone {
          justify-content: center;
        }

        .orther-login-tip {
          font-size: 13px;
        }

        .orther-login-link {
          width: 400px;
          display: flex;
          font-size: 13px;
          justify-content: space-around;

          .iconfont {
            margin-right: 6px;
            font-size: 30px;
            vertical-align: middle;
          }

          .icon-weixin {
            color: #28c445;
          }

          .icon-weibo {
            color: #ea5d5c;
          }

          .icon-qq {
            color: #68a5e1;
          }
        }
      }
    }

    .login-tip {
      font-size: 13px;
      text-align: center;
      position: absolute;
      bottom: 20px;
      left: 50%;
      transform: translate(-50%);
      span {
        color: #00aeec;
      }
    }
  }
}
</style>
